import { BizDict, type Entry } from "jmash-core";
import { reactive } from "vue";
import { jobDefineApi } from "./job-define";
import type { JobDefineModel, JobDefineReq } from "./job-define/types";

// 根据key取出Value
export class CustomBizDict {
  values = reactive([] as Entry[]);
  // 查询条件
  query: any;
  constructor(results: any, query: any) {
    this.values.length = 0;
    this.values.push(...results.value);
    this.query = query;
  }
  // 根据给定键获取业务数据项的值的方法
  getValue(key: string): string {
    for (const obj of this.values) {
      if (obj.key == key) {
        return obj.value;
      }
    }
    return "";
  }
}
//获取分类接口
export class JobClassifyDict extends BizDict {
  // 实现 useValues 方法
  useValues(): Promise<Entry[]> {
    return new Promise((resolve, reject) => {
      jobDefineApi
        .getClassify()
        .then(({ data: { values } }) => {
          this.values.length = 0;
          const items: Array<Entry> = values;
          this.values.push(...items.map((item) => item));
          resolve(this.values);
        })
        .catch(reject);
    });
  }
}
//任务名称
export class JobNameBizDict extends BizDict {
  // 实现 useValues 方法
  useValues(): Promise<Entry[]> {
    return new Promise((resolve, reject) => {
      jobDefineApi
        .findList(this.query as JobDefineReq)
        .then(({ data }) => {
          const items: JobDefineModel[] = data.results;
          this.values.length = 0;
          this.values.push(
            ...items.map(
              (item) =>
                ({
                  key: item.jobName,
                  value: item.jobCnName,
                }) as Entry,
            ),
          );
          resolve(this.values);
        })
        .catch(reject);
    });
  }
}
